#!/bin/bash
set -e

echo "start run"

if [ ! -z "$DEBUG" ]; then
  env
  set -x
fi

args=("$@")
readonly WORK_DIR="${args[0]}"

export PATH="${WORK_DIR}:${WORK_DIR}/files/bin:${PATH}"
export XDG_DATA_DIRS="./files/share/:/usr/share/gnome:/usr/local/share/:/usr/share/:${XDG_DATA_DIRS}"
export LD_LIBRARY_PATH="${WORK_DIR}/files/lib:${LD_LIBRARY_PATH}"
export XDG_DATA_DIRS="${WORK_DIR}/files/share/:${XDG_DATA_DIRS}:/usr/share/gnome/:/usr/local/share/:/usr/share/"
export GSETTINGS_SCHEMA_DIR="/usr/share/glib-2.0/schemas:${GSETTINGS_SCHEMA_DIR}"

shift
NUMBER_OF_ARGS="$#"
args=("$@")

BIN="$WORK_DIR/files/bin/demo"

bundle_exec() {
  if [ $NUMBER_OF_ARGS -eq 0 ]; then
    exec "$BIN"
  else
    exec "$BIN" "${args[@]}"
  fi
}

bundle_exec
